package com.zoshine.zzg.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.constant.Session;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.IdAndCount;
import com.zoshine.zzg.pojo.OrderPrisoner;
import com.zoshine.zzg.pojo.OrderPrisonerDetail;
import com.zoshine.zzg.pojo.PrisonerInfo;
import com.zoshine.zzg.pojo.pojostatus.OrderStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.OrderPrisonerDetailService;
import com.zoshine.zzg.service.OrderPrisonerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 点单Controller
 * @author 田沛凌
 * @time 2017年6月26日 下午1:33:47
 */
@RestController
@RequestMapping(value = "/orderPrisoner",method = RequestMethod.POST)
public class OrderPrisonerController extends BaseController {

	private OrderPrisonerService orderPrisonerService;
	private OrderPrisonerDetailService orderPrisonerDetailService;

	@Autowired
	public void setOrderPrisonerService(OrderPrisonerService orderPrisonerService) {
		this.orderPrisonerService = orderPrisonerService;
	}
	@Autowired
	public void setOrderPrisonerDetailService(OrderPrisonerDetailService orderPrisonerDetailService) {
		this.orderPrisonerDetailService = orderPrisonerDetailService;
	}

	/**
	 * 新增点单
	 *
	 */
	@RequestMapping("/addOrderPrisoner")
	public BaseResponse addOrderPrisoner(String idAndCounts){
		System.out.println(idAndCounts);
		List<IdAndCount> list = JSON.parseArray(idAndCounts, IdAndCount.class);
		OrderPrisoner orderPrisoner = new OrderPrisoner();
		PrisonerInfo prisonerInfo = (PrisonerInfo) session.getAttribute(Session.PRISONER_INFO);
		orderPrisoner.setPrisonerInfo(prisonerInfo.getId());
		orderPrisoner.setIdAndCounts(list);
		orderPrisonerService.addOrderPrisoner(orderPrisoner);
		return AppResponse.OK;
	}
	
	/**
	 * 获取单个点单信息
	 * @param id 点单ID
	 * @return 点单对象
	 */
	@RequestMapping("/getOrderPrisoner")
	public BaseResponse getOrderPrisoner(Long id){
		OrderPrisoner orderPrisoner = orderPrisonerService.get(id);
		return new AppResponse(orderPrisoner);
	}
	/**
	 * 获取点单分页信息(初步点单页面)
	 * @param orderPrisoner 点单对象
	 * @return 点单信息
	 */
	@RequestMapping("/pageOrderPrisoner")
	public BaseResponse pageOrderPrisoner(OrderPrisoner orderPrisoner){
		orderPrisoner.setStatus(OrderStatus.PENDINGAUDIR.getValue());
		List<OrderPrisoner> pageOrderPrisonerList = orderPrisonerService.list(orderPrisoner);
		return new AppResponse(new PageInfo<>(pageOrderPrisonerList));
	}
	/**
	 * 获取点单分页信息(历史点单页面)
	 * @param orderPrisoner 点单对象
	 * @return 点单信息
	 */
	@RequestMapping("/pageOrderPrisonerHis")
	public BaseResponse pageOrderPrisonerHis(OrderPrisoner orderPrisoner){
		List<OrderPrisoner> pageOrderPrisonerList = orderPrisonerService.orderPrisonerListHis(orderPrisoner);
		return new AppResponse(new PageInfo<>(pageOrderPrisonerList));
	}
	/**
	 * 获取点单信息
	 * @param orderPrisoner 点单对象
	 * @return 点单集合
	 */
	@RequestMapping("/listOrderPrisoner")
	public BaseResponse listOrderPrisoner(OrderPrisoner orderPrisoner){
		List<OrderPrisoner> pageOrderPrisonerList = orderPrisonerService.list(orderPrisoner);
		return new AppResponse(pageOrderPrisonerList);
	}
	/**
	 * 修改点单
	 * @param orderPrisoner 点单对象
	 */
	@RequestMapping("/editOrderPrisoner")
	public BaseResponse editOrderPrisoner(OrderPrisoner orderPrisoner){
		orderPrisonerService.edit(orderPrisoner);
		return AppResponse.OK;
	}
	/**
	 * 删除点单信息
	 * @param id (点单Id)
	 */
	@RequestMapping("/delOrderPrisoner")
	public BaseResponse delOrderPrisoner(Long id){
		orderPrisonerService.del(id);
		return AppResponse.OK;
	}
	
	/**
	 * 获取点单详情信息
	 * @param id 点单ID
	 * @return 点单详情集合
	 */
	@RequestMapping("/pageOrderPrisonerDetail")
	public BaseResponse pageOrderPrisonerDetail(Long id){
		List<OrderPrisonerDetail> pageOrderPrisonerDetailList = orderPrisonerService.pageOrderPrisonerDetailList(id);
		return new AppResponse(pageOrderPrisonerDetailList);
	}
	
	/**
	 * 删除点单详情信息(单个删除)
	 * @param id (点单详情Id)
	 */
	@RequestMapping("/delOrderPrisonerDetail")
	public BaseResponse delOrderPrisonerDetail(Long id){
		orderPrisonerDetailService.del(id);
		return AppResponse.OK;
	}
	/**
	 * 批量删除点单详情信息(批量删除)
	 * @param IdStr (点单详情Id集合)
	 */
	@RequestMapping("/delOrderPrisonerDetailList")
	public BaseResponse delOrderPrisonerDetailList(String IdStr){
		orderPrisonerDetailService.del(IdStr);
		return AppResponse.OK;
	}
	/**
	 * 修改点单详情
	 * @param orderPrisonerDetail 点单详情对象
	 */
	@RequestMapping("/editOrderPrisonerDetail")
	public BaseResponse editOrderPrisonerDetail(OrderPrisonerDetail orderPrisonerDetail){
	    orderPrisonerDetailService.edit(orderPrisonerDetail);
	    return AppResponse.OK;
	}
	/**
	 * 查询点单详情
	 * @param id 点单详情ID
	 */
	@RequestMapping("/getOrderPrisonerDetail")
	public BaseResponse getOrderPrisonerDetail(Long id){
	    OrderPrisonerDetail orderPrisonerDetail = orderPrisonerDetailService.get(id);
	    return new AppResponse(orderPrisonerDetail);
	}
	/**
	 * 点单审核通过
	 * @param idStr 点单ID集合
	 */
	@RequestMapping("/passOrderPrisoner")
	public BaseResponse passOrderPrisoner(String idStr){
		orderPrisonerService.editOrderPrisonerStatus(idStr, OrderStatus.AUTIDED.getValue());
	    return AppResponse.OK;
	}
	/**
	 * 点单审核拒绝
	 * @param idStr 点单ID集合
	 */
	@RequestMapping("/failOrderPrisoner")
	public BaseResponse failOrderPrisoner(String idStr){
		orderPrisonerService.editOrderPrisonerStatus(idStr, OrderStatus.FAIL.getValue());
	    return AppResponse.OK;
	}

	/**
	 * 犯人收货页面(未收货)
	 * @param prisonerId 犯人ID
	 * @return 点单详情集合
	 */
	@RequestMapping("/pageByPrisonerId")
	public BaseResponse pageByPrisonerId(Long prisonerId){
		if (prisonerId == null){
			throw new AppException(ErrorMessage.PRISONER_ID_CAN_NOT_NULL);
		}
		OrderPrisoner orderPrisoner=new OrderPrisoner();
		orderPrisoner.setPrisonerInfo(prisonerId);
		List<OrderPrisoner> pageOrderPrisonerList = orderPrisonerService.listFinish(orderPrisoner);
		return new AppResponse(new PageInfo<>(pageOrderPrisonerList));
	}
	/**
	 * 犯人收货页面(已收货)
	 * @param prisonerId 犯人ID
	 * @return 点单详情集合
	 */
	@RequestMapping("/pageByPrisonerIdFinish")
	public BaseResponse pageByPrisonerIdFinish(Long prisonerId){
		if (prisonerId == null){
			throw new AppException(ErrorMessage.PRISONER_ID_CAN_NOT_NULL);
		}
		OrderPrisoner orderPrisoner=new OrderPrisoner();
		orderPrisoner.setPrisonerInfo(prisonerId);
		orderPrisoner.setStatus(OrderStatus.SETTLEMENT.getValue());
		List<OrderPrisoner> pageOrderPrisonerList = orderPrisonerService.list(orderPrisoner);
		return new AppResponse(new PageInfo<>(pageOrderPrisonerList));
	}

	/**
	 * 确认收货
	 * @param idStr 点单ID集合
	 */
	@RequestMapping("settlement")
	public BaseResponse settlement(String idStr){
		orderPrisonerService.settlement(idStr, OrderStatus.SETTLEMENT.getValue());
		return AppResponse.OK;
	}
}
